package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Address;
import com.khshop.pojo.Comment;
import com.khshop.service.AddressService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 地址 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-15
 */

@RestController
@RequestMapping("/")
public class AddressController {

    @Autowired
    AddressService addressService;

    //新增地址
    @ApiOperation("新增地址")
    @PostMapping("/addAddress")
    @ResponseBody
    public AjaxResult addAddress(@RequestBody Address address){
        if (addressService.addAddress(address).isFlag()) {
            return  AjaxResult.success();
        } else {
            return  AjaxResult.error();
        }

    }

    //修改地址
    @ApiOperation("修改地址")
    @PostMapping("/updateAddress")
    @ResponseBody
    public AjaxResult updateAddress(@RequestBody Address address){
        if (addressService.updateAddress(address).isFlag()) {
            return  AjaxResult.success();
        } else {
            return  AjaxResult.error();
        }

    }

    //删除地址
    @ApiOperation("删除地址")
    @PostMapping("/delAddress")
    @ResponseBody
    public AjaxResult delAddress(Long id){
        if (addressService.delAddress(id).isFlag()) {
            return  AjaxResult.success();
        } else {
            return  AjaxResult.error();
        }

    }

    //设置默认地址
    @ApiOperation("设置默认地址")
    @PostMapping("/setDefaultAddress")
    @ResponseBody
    public AjaxResult setDefaultAddress(Long id){
        if (addressService.setDefaultAddress(id).isFlag()) {
            return  AjaxResult.success();
        } else {
            return  AjaxResult.error();
        }

    }

    //查询地址
    @ApiOperation("查询地址")
    @PostMapping("/searchAddress")
    @ResponseBody
    public AjaxResult searchAddress(@RequestParam("userid")Long userid,
                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                    @RequestParam(value = "limit",defaultValue = "15") Integer limit){

        ServiceResult<IPage<Address>> serviceResult = addressService.searchAddress(userid,page, limit);
        if (serviceResult.isFlag()) {
            return  AjaxResult.success( (int) serviceResult.getData().getTotal(),serviceResult.getData());
        } else {
            return  AjaxResult.error();
        }

    }
}

